package com.tosit.bmx.common.shiro;

import com.tosit.bmx.domian.User;
import com.tosit.bmx.service.user.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListenerAdapter;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * session清除时回掉
 *
 * @author tsl
 * @create 2018-05-05 11:40
 **/
@Controller
public class ShiroSessionListener extends SessionListenerAdapter {
    @Autowired
    UserService userService;
    @Override
    public void onExpiration(Session session) {//会话过期时触发
        Subject subject = SecurityUtils.getSubject();
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        if(user!=null && user.getIsOnline()!=0){
            System.out.println("!!！！！");
            user.setIsOnline(0);
            userService.modifyUser(user);
        }
    }

    @Override
    public void onStart(Session session) {//会话创建时触发
        System.out.println("onStart");
    }

    @Override
    public void onStop(Session session) {//退出时触发
        System.out.println("onStop");
    }
}
